package nodes;

import compiler.Compiler;
import compiler.SymbolTable;

import descriptors.AbstractDescr;
import descriptors.VarDescr;

public class VarNode extends AbstractNode {

	ListNode<IdentNode> identList;
	AbstractNode type;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public VarNode(ListNode<IdentNode> identList, AbstractNode type, int line, int column){
		super(line,column);
		this.identList=identList;
		this.type=type;
	}
	
	@Override
	public AbstractDescr compile(
			SymbolTable symbolTable, Compiler compiler) {
		AbstractDescr typeDescr=null;
		if(type instanceof IdentNode){
			typeDescr=symbolTable.search(compiler.level(),((IdentNode) type).ident());
		}
		else {
			typeDescr=type.compile(symbolTable,compiler);
		}
		for(IdentNode node:identList){
			symbolTable.set(node.ident(),new VarDescr(compiler.level(),compiler.allocateMem(typeDescr),false,typeDescr));
		}
		return null;
	}

	@Override
	public void print() {
		trace("VarNode");
		identList.print();
		type.print();
		unindent();
	}

}
